package com.logindemo.servlet; /**
 * @author wzm
 * @create 2023-03-07 14:15
 */

import com.logindemo.entity.User;
import com.logindemo.service.UserSerivce;
import com.logindemo.service.impl.UserServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/registerServlet")
public class RegisterServlet extends HttpServlet {
    private UserSerivce userSerivce = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("注册");

        //设置编码
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        //获取请求参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        //将参数封装为对象
        User user = new User(username,password);

        //调用Service执行功能操作
       boolean flag =  userSerivce.register(user);

       //对结果判断并跳转
        if(flag){
            //注册成功
            response.getWriter().write(username+"注册成功！");
        }else{
            //失败
            response.getWriter().write("内部错误，注册失败！");
        }


    }
}
/*
* 模拟练习：
* 做一个页面  有一个搜索框 一个搜索按钮   输出相关的值  模糊搜索内容并响应到浏览器上
* */